package com.zgs.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.zgs.mall.product.entity.AttrEntity;
import com.zgs.mall.product.service.AttrAttrgroupRelationService;
import com.zgs.mall.product.service.AttrService;
import com.zgs.mall.product.service.CategoryService;
import com.zgs.mall.product.vo.AttrGroupRelationVo;
import com.zgs.mall.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zgs.mall.product.entity.AttrGroupEntity;
import com.zgs.mall.product.service.AttrGroupService;
import com.zgs.common.utils.PageUtils;
import com.zgs.common.utils.R;



/**
 * 属性分组
 *
 * @author zgs
 * @email 
 * @date 2020-07-09 21:26:38
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    ///product/attrgroup/{catelogId}/withattr获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo>  list =attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }


    ///product/attrgroup/attr/relation 添加属性与分组关联关系
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody  List<AttrGroupRelationVo> attrGroupRelationVos){
        attrAttrgroupRelationService.saveAttrRelation(attrGroupRelationVos);
        return R.ok();

    }
    ///product/attrgroup/attr/relation/delete 删除属性与分组的关联关系
    @PostMapping("/attr/relation/delete")
    public R deleteRelationDelete(@RequestBody  AttrGroupRelationVo[] attrGroupRelationVos){
        attrService.deleteRelationDelete(attrGroupRelationVos);
        return R.ok();

    }
    ///product/attrgroup/{attrgroupId}/attr/relation
    //属性分组和关联的属性(规格参数)
    @RequestMapping("/{attrgroupId}/attr/relation")
    public  R attrRelation(@PathVariable("attrgroupId")  Long attrgroupId){
        List<AttrEntity>  list =attrService.getRelation(attrgroupId);
        return R.ok().put("data",list);
    }

    ////product/attrgroup/{attrgroupId}/noattr/relation
    //、获取属性分组没有关联的其他属性
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public  R attrNoRelation(@PathVariable("attrgroupId")  Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils page =attrService.getAttrNoRelation(attrgroupId,params);
        return R.ok().put("page",page);

    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
       // PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
   // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath= categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
  //  @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
